package kyle.test;


import java.io.File;

import org.json.JSONException;
import org.json.JSONObject;

public class ServerConnection {

	public class ServerURL {
		public static final String mServerAddress = "http://121.40.142.208:8080/AppServer/";
		public static final String GET_CODE = mServerAddress + "getCode.html";
		public static final String REGISTER = mServerAddress + "register.html";
		public static final String LOGIN = mServerAddress + "login.html";
		public static final String UPDATE_PASSWORD = mServerAddress
				+ "updatepassword.html";
		public static final String UPDATE_NICK = mServerAddress
				+ "updatenick.html";
		public static final String UPDATE_HEAD = mServerAddress
				+ "updatehead.html";
		public static final String SET_PATTERN = mServerAddress
				+ "setpattern.html";
		public static final String GET_PATTERN = mServerAddress
				+ "getpattern.html";
		public static final String GET_FAQ = mServerAddress + "getfaq.html";
		public static final String FEEDBACK = mServerAddress + "feedback.html";
		public static final String SUBMIT_TRAVEL = mServerAddress
				+ "submittravel.html";
		public static final String GET_FEEDBACK_BY_DATE = mServerAddress
				+ "getfeedbackbydate.html";
		public static final String GET_FEEDBACK_ALL = mServerAddress
				+ "getfeedbackall.html";
		public static final String GET_APP = mServerAddress + "getapp.html";
		public static final String GET_ROM = mServerAddress + "getrom.html";
		public static final String GET_BRALD = mServerAddress + "getbrald.html";
		public static final String GET_HOT_BRALD = mServerAddress
				+ "gethotbrald.html";
		public static final String GET_SERIES = mServerAddress
				+ "getseries.html";
		public static final String GET_MODEL = mServerAddress + "getmodel.html";
		public static final String SUBMIT_MODEL = mServerAddress
				+ "submitmodel.html";
		public static final String SUBMIT_DEVICE = mServerAddress
				+ "submitdevice.html";
		public static final String UPDATE_DEVICE_NAME = mServerAddress
				+ "updatedevicename.html";
		public static final String UPDATE_DEVICE_TYPE = mServerAddress
				+ "updatevehicletype.html";
		public static final String UPDATE_CENTRALAXIS = mServerAddress
				+ "updatecentralaxis.html";
		public static final String GET_DEVICE_BY_USER = mServerAddress
				+ "getdevicebyuser.html";
		public static final String GET_USER_BY_DEVICE = mServerAddress
				+ "getuserbydevice.html";
		public static final String GET_WITHS_BY_USERDEVICE = mServerAddress
				+ "getwithsbyuserdevice.html";
		public static final String RQAMBIENT = mServerAddress
				+ "reqambient.html";
	}

	public ServerConnection(ResponseHandler handler) {
	}

	public static String GetCode(String phone) {
		JSONObject object = new JSONObject();
		try {
			object.put("phone", phone);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_CODE,
				object.toString());
	}

	public static String Register(String phonenumber, String password,
			String code, File head) {
		JSONObject object = new JSONObject();
		try {
			object.put("password", password);
			object.put("phone", phonenumber);
			object.put("code", code);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithMultiData(ServerURL.REGISTER,
				object.toString(),head);
	}

	public static String Login(String phone, String password) {
		JSONObject object = new JSONObject();
		try {
			object.put("phone", phone);
			object.put("password", password);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.LOGIN, object.toString());
	}

	public static String UpdatePassword(String ciphertext, String newpassword,
			String password, String phone, String userid, String code) {
		JSONObject object = new JSONObject();
		try {
			object.put("phone", phone);
			object.put("password", password);
			object.put("ciphertext", ciphertext);
			object.put("newpassword", newpassword);
			object.put("userid", userid);
			object.put("code", code);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.UPDATE_PASSWORD,
				object.toString());
	}

	public static String UpdateNick(String nick, String userid) {
		JSONObject object = new JSONObject();
		try {
			object.put("nick", nick);
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.UPDATE_NICK,
				object.toString());
	}

	public static String UpdateHead(String userid, File file) {
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithMultiData(ServerURL.UPDATE_HEAD,
				object.toString(),file);
	}

	public static String SetPattern(String userid, String userpattern) {
		JSONObject object = new JSONObject();
		try {
			object.put("userpattern", userpattern);
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.SET_PATTERN,
				object.toString());
	}

	public static String GetPattern(String userid) {
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_PATTERN,
				object.toString());
	}

	public static String GetFaq(String userid) {
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_FAQ,
				object.toString());
	}

	public static String FeedBack(String appversion, String message,
			String userid) {
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
			object.put("appversion", appversion);
			object.put("message", message);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.FEEDBACK,
				object.toString());
	}

	public static String SubmitTravel(String travelid, String starttime,
			String createtime, String mileage, String userid, String velocity,
			int eventstate) {
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
			object.put("travelId",travelid);
			object.put("startingtime", starttime);
			object.put("createtime", createtime);
			object.put("mileage", mileage);
			object.put("velocity", velocity);
			object.put("eventstate", eventstate);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.SUBMIT_TRAVEL,
				object.toString());
	}
	public static String GetFeedBackByDate(String createtime,String userid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
			object.put("createtime", createtime);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_FEEDBACK_BY_DATE, object.toString());
	}
	public static String GetFeedbackAll(String userid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_FEEDBACK_ALL, object.toString());
	}
	public static String GetApp(String userid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_APP, object.toString());
	}
	public static String GetRom(String deviceid,String userid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
			object.put("deviceid", deviceid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_ROM, object.toString());
	}
	public static String GetBrald(String userid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_BRALD, object.toString());
	}
	public static String GetHotBrald(String userid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_HOT_BRALD, object.toString());
	}
	public static String GetSeries(String userid,String braid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
			object.put("braid", braid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_SERIES, object.toString());
	}
	public static String GetModel(String serid,String userid){
		JSONObject object = new JSONObject();
		try {
			object.put("userid", userid);
			object.put("serid", serid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return HttpRequest.doPostWithJSON(ServerURL.GET_MODEL, object.toString());
	}
	public interface ResponseHandler {
		public void handleResponse(int request_code, String response);
	}
}
